﻿Imports System.Data.OleDb

Public Class PlayQuizQuestions
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try

            If (Not Page.IsPostBack) Then

                If (Session("quizId") <> Nothing) Then
                    ViewState("quizId") = Convert.ToInt32(Session("quizId"))
                    Session("quizScore") = 0

                    getData()
                    ViewState("questionIndex") = 0

                    Dim answers As ArrayList = New ArrayList()
                    ViewState("answers") = answers

                    getQuestion()

                Else
                    Response.Redirect("~/Default.aspx")
                End If
            End If


        Catch ex As Exception

        End Try

    End Sub

    Private Sub getQuestion()
        Try


            Dim availableQuestions As ArrayList = ViewState("availableQuestions")
            Dim currentIndex As Int32 = ViewState("questionIndex")

            If (currentIndex <= availableQuestions.Count - 1) Then
                'get next question
                Dim selectedQuestion As Quiz_QuestionManagement = New Quiz_QuestionManagement()
                Dim dr As OleDbDataReader = selectedQuestion.SelectQuiz_QuestionByQuizQuestionId(availableQuestions(currentIndex))

                Dim possibleAnswer1 As ListItem
                Dim possibleAnswer2 As ListItem
                Dim possibleAnswer3 As ListItem
                Dim possibleAnswer4 As ListItem

                If dr.HasRows Then
                    dr.Read()
                    If Not IsDBNull(dr("Quiz_QuestionContent")) Then lblQuestion.Text = (dr("Quiz_QuestionContent"))
                    If Not IsDBNull(dr("Quiz_QuestionPictureUrl")) Then imgQuestion.ImageUrl = (dr("Quiz_QuestionPictureUrl"))
                    If Not IsDBNull(dr("Quiz_QuestionPossibleAnswer1")) Then possibleAnswer1 = New ListItem(dr("Quiz_QuestionPossibleAnswer1"), "Quiz_QuestionPossibleAnswer1")
                    If Not IsDBNull(dr("Quiz_QuestionPossibleAnswer2")) Then possibleAnswer2 = New ListItem(dr("Quiz_QuestionPossibleAnswer2"), "Quiz_QuestionPossibleAnswer2")
                    If Not IsDBNull(dr("Quiz_QuestionPossibleAnswer3")) Then possibleAnswer3 = New ListItem(dr("Quiz_QuestionPossibleAnswer3"), "Quiz_QuestionPossibleAnswer3")
                    If Not IsDBNull(dr("Quiz_QuestionPossibleAnswer4")) Then possibleAnswer4 = New ListItem(dr("Quiz_QuestionPossibleAnswer4"), "Quiz_QuestionPossibleAnswer4")

                End If

                rblAnswer.Items.Clear()


                rblAnswer.Items.Add(possibleAnswer1)
                rblAnswer.Items.Add(possibleAnswer2)
                rblAnswer.Items.Add(possibleAnswer3)
                rblAnswer.Items.Add(possibleAnswer4)

                lblQuestionNumber.Text = "Question " + (currentIndex + 1).ToString()

                currentIndex = currentIndex + 1
                ViewState("questionIndex") = currentIndex

            Else
                pnlQuestions.Visible = False
                pnlAnswers.Visible = True
                ViewState("answerIndex") = 0

                Dim selectedQuestion As Quiz_QuestionManagement = New Quiz_QuestionManagement()
                Dim dr As OleDbDataReader = selectedQuestion.SelectQuiz_QuestionByQuizId(ViewState("quizId"))

                rptResults.DataSource = dr
                rptResults.DataBind()

            End If
        Catch ex As Exception

        End Try
    End Sub

    Protected Function checkAnswer(ByVal quizQuestionId As String)
        Try

            Dim answers As ArrayList = ViewState("answers")
            Dim currentIndex As Int32 = ViewState("answerIndex")
            Dim currentAnswer As String = answers(currentIndex)
            'get question
            Dim currentQuestion As Quiz_QuestionManagement = New Quiz_QuestionManagement()
            Dim dr As OleDbDataReader = currentQuestion.SelectQuiz_QuestionByQuizQuestionId(quizQuestionId)

            Dim correctAnswer As String = String.Empty
            If dr.HasRows Then
                dr.Read()
                If Not IsDBNull(dr("Quiz_QuestionCorrectAnswer")) Then correctAnswer = (dr("Quiz_QuestionCorrectAnswer"))
            End If

            'currentIndex = currentIndex + 1
            'ViewState("answerIndex") = currentIndex

            If (correctAnswer = currentAnswer) Then

                Dim totalScore As Int32 = Convert.ToInt32(Session("quizScore"))
                totalScore = totalScore + 1
                Session("quizScore") = totalScore


                Return "Pictures/Quiz/quizCorrect.png"
            Else
                Return "Pictures/Quiz/quizWrong.png"
            End If

        Catch ex As Exception
            Return Nothing
        End Try
    End Function

    Protected Function getTotalScore()
        Try


            Dim totalScore As Int32 = Convert.ToInt32(Session("quizScore"))
            Dim insertQuizScore As QuizScoreManagement = New QuizScoreManagement()
            Dim userId As Int32 = New aspnet_UsersManagement().SelectUserIDByUsername(User.Identity.Name)
            insertQuizScore.InsertQuizScore(Convert.ToInt32(Session("quizId")), userId, totalScore)
            Return totalScore
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

    Protected Function writeCorrectAnswer(ByVal quizQuestionId As String)
        Try

            Dim answers As ArrayList = ViewState("answers")
            Dim currentIndex As Int32 = ViewState("answerIndex")
            Dim currentAnswer As String = answers(currentIndex)
            'get question
            Dim currentQuestion As Quiz_QuestionManagement = New Quiz_QuestionManagement()
            Dim dr As OleDbDataReader = currentQuestion.SelectQuiz_QuestionByQuizQuestionId(quizQuestionId)

            Dim correctAnswer As String = String.Empty
            If dr.HasRows Then
                dr.Read()
                If Not IsDBNull(dr("Quiz_QuestionCorrectAnswer")) Then correctAnswer = (dr("Quiz_QuestionCorrectAnswer"))
            End If

            currentIndex = currentIndex + 1
            ViewState("answerIndex") = currentIndex

            If (correctAnswer = currentAnswer) Then
                Return ""
            Else
                Dim currentQuestion2 As Quiz_QuestionManagement = New Quiz_QuestionManagement()
                Dim dr2 As OleDbDataReader = currentQuestion2.SelectQuiz_QuestionByQuizQuestionId(quizQuestionId)

                Dim selectedAnswer As String = String.Empty
                If dr2.HasRows Then
                    dr2.Read()
                    If Not IsDBNull(dr2(correctAnswer)) Then selectedAnswer = (dr2(correctAnswer))
                End If

                Return selectedAnswer
            End If


        Catch ex As Exception
            Return Nothing
        End Try
    End Function

    Protected Function getAnswer(ByVal quizQuestionId As String)
        Try

            Dim answers As ArrayList = ViewState("answers")
            Dim currentIndex As Int32 = ViewState("answerIndex")
            Dim currentAnswer As String = answers(currentIndex)
            'get question
            Dim currentQuestion As Quiz_QuestionManagement = New Quiz_QuestionManagement()
            Dim dr As OleDbDataReader = currentQuestion.SelectQuiz_QuestionByQuizQuestionId(quizQuestionId)

            Dim selectedAnswer As String = String.Empty
            If dr.HasRows Then
                dr.Read()
                If Not IsDBNull(dr(currentAnswer)) Then selectedAnswer = (dr(currentAnswer))
            End If


            Return selectedAnswer

        Catch ex As Exception
            Return Nothing
        End Try
    End Function

    Private Sub getData()
        Try

        
        Dim selectedQuestion As Quiz_QuestionManagement = New Quiz_QuestionManagement()
        Dim dr As OleDbDataReader = selectedQuestion.SelectQuiz_QuestionByQuizId(ViewState("quizId"))

        Dim availableQuestions As ArrayList = New ArrayList()
        ViewState("availableQuestions") = availableQuestions

        If dr.HasRows Then
            Do While dr.Read()
                If Not IsDBNull(dr("Quiz_QuestionId")) Then availableQuestions.Add(dr("Quiz_QuestionId"))
            Loop
        End If
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub btnAnswer_Click(sender As Object, e As EventArgs) Handles btnAnswer.Click
        Try

            If (rblAnswer.SelectedValue <> Nothing) Then
                Dim answers As ArrayList = ViewState("answers")
                answers.Add(rblAnswer.SelectedValue)
                ViewState("answers") = answers

                getQuestion()
            Else
                lblMessage.Text = "Please select an answer."
            End If



        Catch ex As Exception

        End Try
    End Sub
End Class